/*
 * This handles what happens when a private message is clicked on
 * for viewing
 */

package airport.forum.action;

import java.util.Date;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;

import airport.forum.dao.PrivateMessageDAO;
import airport.forum.pojo.PrivateMessage;

public class ViewPrivateMessageAction extends ActionSupport implements ServletResponseAware,
ServletRequestAware {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int messageId;
	private String sender = null;
	private Date dateCreated = null;
	private String title = null;
	private String body = null;
	private PrivateMessage pm = new PrivateMessage();
	private String username = null;
	
	protected HttpServletResponse servletResponse;

	@Override
	public void setServletResponse(HttpServletResponse servletResponse) {
		this.servletResponse = servletResponse;
	}

	protected HttpServletRequest servletRequest;

	@Override
	public void setServletRequest(HttpServletRequest servletRequest) {
		this.servletRequest = servletRequest;
	}

	public String execute() {
		//need userName
		Cookie[] cookies = servletRequest.getCookies();
		String cookieName = "AirportUser";
		if (cookies != null) {
			for (int i = 0; i < cookies.length; i++) {
				if (cookies[i].getName().equals(cookieName)) {
					username = cookies[i].getValue();
					break;
				}
			}
		}
		
		PrivateMessageDAO pmDAO = new PrivateMessageDAO();
		
		pm = pmDAO.getPm(messageId);
		title = pm.getTitle();
		body = pm.getBody();
		sender = pm.getSender();
		dateCreated = pm.getDateCreated();
		System.out.println("PM set");
		
		//set message to read if it hasn't been already
		if(pm.getRead() == false) {
			pmDAO.setMessageToRead(messageId);
		}
		
		return "success";
	}
	

	public int getMessageId() {
		return messageId;
	}

	public void setMessageId(int messageId) {
		this.messageId = messageId;
	}

	public PrivateMessage getPm() {
		return pm;
	}

	public void setPm(PrivateMessage pm) {
		this.pm = pm;
	}

	public String getSender() {
		return sender;
	}

	public void setSender(String sender) {
		this.sender = sender;
	}

	public Date getDateCreated() {
		return dateCreated;
	}

	public void setDateCreated(Date dateCreated) {
		this.dateCreated = dateCreated;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getBody() {
		return body;
	}

	public void setBody(String body) {
		this.body = body;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}
	
	
}
